﻿using AutoMapper;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Read.Apilication.Common.Delparment;
using B.S.BasicData.Read.DTOS;
using MediatR;

namespace B.S.BasicData.Read.Apilication.Handler.Delparment
{
    public class GetoneRoleCommandHandle:IRequestHandler<GetoneRoleCommand,ApiResult<List<SetuserDTO>>>
    {
        private readonly IBaseRepository<UserModel> UserDal;
        private readonly IBaseRepository<AccountRoleModel> AccouDal;
        private readonly IMapper mapper;
        public GetoneRoleCommandHandle(IBaseRepository<UserModel> userDal, IBaseRepository<AccountRoleModel> accouDal, IMapper mapper)
        {
            UserDal = userDal;
            AccouDal = accouDal;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<SetuserDTO>>> Handle(GetoneRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<SetuserDTO>> result = new ApiResult<List<SetuserDTO>>();

            var res = AccouDal.GetAll().Where(x => x.RoleId == request.RoleId).Select(x => x.UserId);
            var userlist = UserDal.GetAll().Where(x => res.Contains(x.UserId));

            var model = mapper.Map<List<SetuserDTO>>(userlist);

            result.Code = ApiEnum.Success;
            result.Message = "显示成功";
            result.Data = model;
            return Task.FromResult(result);

        }





    }
}
